package com.corx.network.mysqlstorage.repository;

import com.corx.network.mysqlstorage.common.JdbcPagingSupport;
import com.corx.network.mysqlstorage.common.PageQuery;
import com.corx.network.mysqlstorage.common.PageResult;
import com.corx.network.mysqlstorage.common.SqlQueryBuilder;
import com.corx.network.mysqlstorage.model.DeviceModel;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * @author gx
 * @version 1.0.0
 */
@Repository
public class JdbcDeviceModelRepository extends JdbcPagingSupport<DeviceModel> implements DeviceModelRepository {

    public JdbcDeviceModelRepository(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    protected RowMapper<DeviceModel> getRowMapper() {
        return (rs, rowNum) -> {
            DeviceModel data = new DeviceModel();
            data.setId(rs.getLong("id"));
            data.setName(rs.getString("name"));
            data.setCode(rs.getString("code"));
            data.setDescription(rs.getString("description"));
            data.setCreateTime(rs.getTimestamp("create_time"));
            data.setUpdateTime(rs.getTimestamp("update_time"));
            return data;
        };
    }


    @Override
    public void save(@NonNull DeviceModel model) {
        String sql = "INSERT INTO corx_device_model (`name`,`code`, description, create_time, update_time) VALUES (?, ?, ?, NOW(), NOW())";
        jdbcTemplate.update(sql, model.getName(), model.getCode(), model.getDescription());
    }

    @Override
    public void update(@NonNull DeviceModel model) {
        String sql = "UPDATE corx_device_model SET `name` = ?, `code` = ?,  description = ?, update_time = NOW() WHERE id = ?";
        jdbcTemplate.update(sql, model.getName(), model.getCode(), model.getDescription(), model.getId());
    }

    @Override
    public void delete(@NonNull Long id) {
        jdbcTemplate.update("DELETE FROM corx_device_model WHERE id = ?", id);
    }

    @Override
    public Optional<DeviceModel> findById(@NonNull Long id) {
        List<DeviceModel> list = jdbcTemplate.query("SELECT * FROM corx_device_model WHERE id = ?", getRowMapper(), id);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }

    @Override
    public PageResult<DeviceModel> findByPage(DeviceModel model, PageQuery pageQuery) {

        SqlQueryBuilder queryBuilder = new SqlQueryBuilder("SELECT * FROM corx_device_model");
        if (model.getName() != null && !model.getName().isEmpty()) {
            queryBuilder.whereLike("name", model.getName());
        }
        if (model.getCode() != null && !model.getCode().isEmpty()) {
            queryBuilder.whereLike("code", model.getCode());
        }
        if (pageQuery.hasSort()) {
            queryBuilder.orderBy(pageQuery.orderBy());
        }
        // 构建 SQL 和参数
        String baseSql = queryBuilder.getSql();
        Object[] params = queryBuilder.getParams();

        return super.findByPage(baseSql, params, pageQuery);
    }


}
